import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_app_bar.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_empty.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_scaffold.dart';
import 'package:jet_rails/jr_app/jr_widgets/jr_user_tile.dart';
import 'package:jet_rails/jr_services/jr_services.dart';

class JrFollowListView extends StatefulWidget {
  const JrFollowListView({Key? key}) : super(key: key);

  @override
  State<JrFollowListView> createState() => _JrFollowListViewState();
}

class _JrFollowListViewState extends State<JrFollowListView> {
  final service = Get.find<JrServices>();
  EasyRefreshController easyRefreshController = EasyRefreshController(
    controlFinishRefresh: true,
    controlFinishLoad: true,
  );
  @override
  Widget build(BuildContext context) {
    return JrScaffold(
        appBar: JrAppBar(
          title: 'Follow List',
        ),
        body: Obx(
          () {
            final list = service.followedList;
            return list.isEmpty
                ? const JrEmpty()
                : Padding(
                    padding: EdgeInsets.symmetric(horizontal: 20.w),
                    child: EasyRefresh(
                        controller: easyRefreshController,
                        onRefresh: () async {
                          await service.refreshFollowedList();
                          easyRefreshController.finishRefresh();
                        },
                        child: ListView.builder(
                            padding: EdgeInsets.zero,
                            itemCount: list.length,
                            itemBuilder: (_, index) {
                              return Container(
                                margin: EdgeInsets.only(top: 30.h),
                                child: JrUserTile(
                                  user: list[index],
                                  action: GestureDetector(
                                    onTap: () => service.unFollow(list[index].userId),
                                    child: Container(
                                      height: 26.w,
                                      width: 72.h,
                                      decoration: BoxDecoration(
                                        borderRadius: BorderRadius.circular(13.w),
                                        gradient: const LinearGradient(
                                            colors: [Color(0xFF955FB5), Color(0xFF955FB5)],
                                            begin: Alignment.centerLeft,
                                            end: Alignment.centerRight),
                                      ),
                                      child: Center(
                                        child: Text(
                                          'Unfollow',
                                          style: TextStyle(fontSize: 12.sp, color: Colors.white),
                                        ),
                                      ),
                                    ),
                                  ),
                                ),
                              );
                            })),
                  );
          },
        ));
  }
}
